package com.nearby.demand.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.easemob.chat.ImageMessageBody;
import com.easemob.chat.TextMessageBody;
import com.easemob.util.DateUtils;
import com.nearby.demand.R;

import java.util.Date;
import java.util.List;

/**
 * Created by lizhangqu on 2015/6/home_3.
 */
public class ConversationsAdapter extends BaseRecyclerViewAdapter<EMConversation, ConversationsAdapter.CViewHolder> {
    private  Context context;
    public ConversationsAdapter(List<EMConversation> list,Context context) {
        super(list);
        this.context = context;
    }

    /**
     * 根据消息内容和消息类型获取消息内容提示
     *
     * @param message
     * @param context
     * @return
     */
    private String getMessageDigest(EMMessage message, Context context) {
        String digest = "";
        switch (message.getType()) {
            case LOCATION: // 位置消息
                if (message.direct == EMMessage.Direct.RECEIVE) {
                    digest = getString(context, R.string.location_recv);
                    String name = message.getFrom();
//                    if (GloableParams.UserInfos != null) {
//                        User user = GloableParams.Users.get(message.getFrom());
//                        if (user != null && null != user.getUserName())
//                            name = user.getUserName();
//                    }
                    digest = String.format(digest, name);
                    return digest;
                } else {
                    digest = getString(context, R.string.location_prefix);
                }
                break;
            case IMAGE: // 图片消息
                ImageMessageBody imageBody = (ImageMessageBody) message.getBody();
                digest = getString(context, R.string.picture)
                        + imageBody.getFileName();
                break;
            case VOICE:// 语音消息
                digest = getString(context, R.string.voice_msg);
                break;
            case VIDEO: // 视频消息
                digest = getString(context, R.string.video);
                break;
            case TXT: // 文本消息
                if (true) {
                    TextMessageBody txtBody = (TextMessageBody) message.getBody();
                    digest = txtBody.getMessage();
                } else {
                    TextMessageBody txtBody = (TextMessageBody) message.getBody();
                    digest = getString(context, R.string.voice_call)
                            + txtBody.getMessage();
                }
                break;
            case FILE: // 普通文件消息
                digest = getString(context, R.string.file);
                break;
            default:
                System.err.println("error, unknow type");
                return "";
        }
        return digest;
    }
    String getString(Context context, int resId) {
        return context.getResources().getString(resId);
    }
    @Override
    protected void bindDataToItemView(CViewHolder myViewHolder, EMConversation item) {
        myViewHolder.setText(R.id.conversations_txt_name, item.getUserName());
        myViewHolder.setText(R.id.conversations_txt_state, item.getType()+"");
//        myViewHolder.setText(R.id.txt_content, SmileUtils.getSmiledText(context,
//                getMessageDigest(lastMessage, context));
        EMMessage lastMessage = item.getLastMessage();
        myViewHolder.setText(R.id.conversations_txt_content,
                getMessageDigest(lastMessage, context));
        myViewHolder.setText(R.id.conversations_txt_time, DateUtils.getTimestampString(new Date(
                item.getLastMessage().getMsgTime())));
    }


    @Override
    public CViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
        return new CViewHolder(inflateItemView(viewGroup, R.layout.ui_conversations_item));

    }

    public class CViewHolder extends BaseRecyclerViewAdapter.SparseArrayViewHolder {

        public CViewHolder(View itemView) {
            super(itemView);
        }


    }

}
